package com.xique.park.controller.web;

import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.domain.PageResult;
import com.xique.common.log.annotation.Log;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.park.bean.request.client.OpenGateRequest;
import com.xique.park.bean.request.web.onDuty.OnDutySettlementRequest;
import com.xique.park.service.service.ExportService;
import com.xique.park.service.service.IOnDutySettlementService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 值班结算controller
 *
 * @author wld
 * @date 2022/3/17 11:04
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/web/onDuty")
public class WebOnDutySettlementController {

    private final IOnDutySettlementService onDutySettlementService;

    private static final String CLIENT_USER_IS_NULL = "岗亭用户不可为空";

    @Autowired
    private ExportService exportService;

    @Log(title = "分页查询值班结算列表")
    @PreAuthorize(hasPermi = "parking:settlement:list")
    @PostMapping(value = "/list")
    public AjaxResult list(@RequestBody(required = false) OnDutySettlementRequest request) {
        request = checkParam(request);
        PageResult result = onDutySettlementService.selectList(request);
        return AjaxResult.success(result);
    }

    /**
     * 结算
     */
    @Log(title = "值班结算")
    @PreAuthorize(hasPermi = "parking:settlement:pay")
    @PutMapping(value = "/settlement")
    public AjaxResult settlement(@RequestBody OnDutySettlementRequest request) {
        Long clientUserId = request.getClientUserId();
        if (clientUserId == null) {
            return AjaxResult.error(CLIENT_USER_IS_NULL);
        }
        onDutySettlementService.updateCashSettlementFeeByClientUserId(clientUserId);
        return AjaxResult.success();
    }

    @Log(title = "值班结算-查看详情-现金收费放行")
    @PreAuthorize(hasPermi = "parking:settlement:list")
    @PostMapping(value = "/noChargeList")
    public AjaxResult noChargeList(@RequestBody OnDutySettlementRequest request) {
        Long clientUserId = request.getClientUserId();
        if (clientUserId == null) {
            return AjaxResult.error(CLIENT_USER_IS_NULL);
        }
        checkParam(request);
        PageResult result = onDutySettlementService.selectNoChargeList(request);
        return AjaxResult.success(result);
    }

    @Log(title = "值班结算-查看详情-免费放行列表")
    @PreAuthorize(hasPermi = "parking:settlement:list")
    @PostMapping(value = "/freeList")
    public AjaxResult freeList(@RequestBody OnDutySettlementRequest request) {
        Long clientUserId = request.getClientUserId();
        if (clientUserId == null) {
            return AjaxResult.error(CLIENT_USER_IS_NULL);
        }
        checkParam(request);
        PageResult result = onDutySettlementService.selectFreeList(request);
        return AjaxResult.success(result);
    }

    @Log(title = "值班结算-查看详情-现金收费放行-导出")
    @PreAuthorize(hasPermi = "parking:settlement:export")
    @PostMapping(value = "/export")
    public void exportNoChargeList(@RequestBody OnDutySettlementRequest request,
                                   HttpServletResponse response) throws IOException {
        Long clientUserId = request.getClientUserId();
        if (clientUserId != null) {
            checkParam(request);
            exportService.exportNoChargeList(request, response);
        }
    }

    @Log(title = "值班结算-查看详情-免费放行-导出")
    @PreAuthorize(hasPermi = "parking:settlement:export")
    @PostMapping(value = "/exportFeeList")
    public void exportFeeList(@RequestBody OnDutySettlementRequest request,
                              HttpServletResponse response) throws IOException {

        checkParam(request);
        exportService.exportFeeList(request, response);
    }


    @Log(title = "值班结算-查看详情-手动开闸-导出")
    @PreAuthorize(hasPermi = "parking:settlement:export")
    @PostMapping(value = "/exportOpenGate")
    public AjaxResult exportOpenGate(@RequestBody OpenGateRequest request,
                                     HttpServletResponse response) throws IOException {
        exportService.exportOpenGate(request, response);
        return AjaxResult.success();
    }


    private OnDutySettlementRequest checkParam(OnDutySettlementRequest request) {
        if (request == null) {
            request = new OnDutySettlementRequest();
        }
        int pageNum = request.getPageNum() == null ? 1 : request.getPageNum();
        int pageSize = request.getPageSize() == null ? 10 : request.getPageSize();
        if (request.getParkId() == null) {
            Long parkId = SecurityUtils.getParkId();
            if (parkId != null) {
                request.setParkId(parkId);
            }
        }
        request.setPageNum(pageNum);
        request.setPageSize(pageSize);
        return request;
    }

}
